<template>
	<section>
		<!--高级查询-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="advancedQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="名称" sortable>
			</el-table-column>
      <el-table-column prop="type" label="营销类型" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.type == 1">市场活动</span>
          <span v-if="scope.row.type == 2">营销活动</span>
        </template>
      </el-table-column>
      <el-table-column prop="dictionaryitem.title" label="优惠方式" sortable>
      </el-table-column>
      <el-table-column prop="startDate" label="开始时间" sortable>
      </el-table-column>
      <el-table-column prop="endDate" label="结束时间" sortable>
      </el-table-column>
      <el-table-column prop="description" label="简介" sortable>
      </el-table-column>

			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
					<el-button type="danger" size="small" @click="handleLog(scope.$index, scope.row)">日志</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--批量删除-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger"
                 @click="batchDelete"
                 :disabled="this.sels.length===0"
                 style="float: left;margin: 20px">
        批量删除</el-button>
      <!--分页条-->
      <el-pagination
          style="float: right;margin: 20px"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.currentPage"
          :page-sizes="pageSizes"
          :page-size="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total">
      </el-pagination>
		</el-col>


    <el-dialog title="操作日志"
               :visible.sync="logFormVisible"
               :close-on-click-modal="false">
      <el-table
          title="操作日志"
          :data="logTable"
          style="width: 100%">
        <el-table-column
            prop="noteContent"
            label="记录">
        </el-table-column>
        <el-table-column
            prop="createTime"
            label="创建时间">
        </el-table-column>
        <el-table-column
            prop="createBy"
            label="创建人">
        </el-table-column>
        <el-table-column
            prop="editTime"
            label="最后一次修改时间">
        </el-table-column>
        <el-table-column
            prop="editBy"
            label="最后一次修改人">
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <!--@click.native vue在自定义组件中使用原生事件要加上native-->
        <el-button @click.native="logFormVisible = false">退出</el-button>
      </div>
    </el-dialog>


		<!--保存界面-->
    <!--
       pros 绑定校验规则名称
       :visible.sync  控制dialog是否显示
       :rules  绑定表单校验规则
       ref="saveForm"  给当前表单取一个别名
    -->
		<el-dialog title="保存"
               :visible.sync="saveFormVisible"
               :close-on-click-modal="false">
			<el-form
          :model="saveForm"
          label-width="80px"
          :rules="saveFormRules"
          ref="saveForm">
				<el-form-item label="名称">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>

				<el-form-item label="营销类型">
            <el-radio v-model="saveForm.type" label="1">市场活动</el-radio>
            <el-radio v-model="saveForm.type" label="2">营销活动</el-radio>
				</el-form-item>
        <el-form-item label="优惠方式">
          <el-select v-model="saveForm.preferentialMethodId" :disabled="this.saveForm.type===1" placeholder="请选择">
            <el-option
                v-for="item in preferentialMethodIds"
                :key="item.id"
                :label="item.title"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
              v-model="saveForm.startDate"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间"
              align="right">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
              v-model="saveForm.endDate"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间"
              align="right">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="简介">
          <el-input  v-model="saveForm.description"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
        <!--@click.native vue在自定义组件中使用原生事件要加上native-->
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">确认</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>

	export default {
		data() {
			return {
        pageInfo:{
          rows: [],
          total: 0,
        },
        pageSizes:[5, 10, 20, 50],
        //查询对象
        query:{
				  //当前页,默认第一页
          currentPage:1,
          //页面显示数据大小,默认显示5条
          pageSize:5,
          //高级查询关键字
          keyword:""
        },
				listLoading: false,
				sels: [],//列表选中列

        saveFormVisible: false,//界面是否显示
        saveLoading: false,
        //校验规则
        saveFormRules: {
					name: [
						{ required: true, message: '请输入名称', trigger: 'blur' },
					],
          sn: [
            { required: true, message: '请输入标识', trigger: 'blur' }
          ]
				},
				//新增界面数据
        saveForm:{
          id:null,
          name:'',
          type: null,
          preferentialMethodId:null,
          startDate:'',
          endDate:'',
          description:'',

          createTime:'',
          createBy:'',
          editTime:'',
          editBy:'',

        },
        activityFormVisible:false,
        typeTree:[],
        preferentialMethodIds:[],//所有优惠方式id

        //日志界面
        logFormVisible:false,
        logTable : []
      }
		},
		methods: {
		  getLogTable(id){
        /*console.log("id");
        console.log(id);
        return */
        this.$http.get("/activityRemark/getRemarkByActivityId/"+id)
            .then(result =>{
              result = result.data;
              this.logTable = result.obj;
            })
            .catch(result =>{

            })
      },

		  getPreferentialMethodIds(){
        this.$http.get("/dictionary/getBySns/activityMethod")
                  .then(result =>{
                    result = result.data;
                    this.preferentialMethodIds = result.obj;
                    /*console.log("title");
                    console.log(this.preferentialMethodIds);*/
                  })
                  .catch(result =>{

                  })
      },

      handleLog(index,row){
        this.logFormVisible = true;
        let id = row.id;
        this.getLogTable(id);
      },
			handleCurrentChange(val) {
			  //改变当前显示页数
        this.query.currentPage = val;
        this.getActivitys();
			},
      handleSizeChange(val) {
			  //改变每页显示条数
        this.query.pageSize = val;
        this.getActivitys();
      },
			//获取列表
			getActivitys() {
				this.listLoading = false;
				this.$http.post("/activity",this.query)
        .then(result => {
          result = result.data;
          console.log(result.obj.total);
          console.log(result.obj.rows);
          this.pageInfo = result.obj;
        })
        .catch(result =>{
          this.$message({
            message: '网络错误!',
            type: 'error'
          });
        })
			},
      advancedQuery(){
        //初始化页面
        this.query.currentPage = 1;
        //加载数据到页面
        this.getActivitys();
      },
			//删除
			handleDel: function (index, row) {
        this.$confirm('数据将无法恢复!', '警告', {
          type: 'warning'
        }).then(() => {
          //弹出加载窗口
          this.listLoading = true;
          this.$http.delete("/activity/"+row.id)
              .then(result => {
                //操作成功进入then,关闭加载窗口
                this.listLoading = false;
                result = result.data;
                if(result.success){
                  this.$message({
                    message: '删除成功!',
                    type: 'success'
                  });
                  //删除完成后刷新页面
                  this.advancedQuery();
                }else{
                  this.$message({
                    message: '删除失败!',
                    type: 'error'
                  });
                }
              })
              .catch(result => {
                this.$message({
                  message: '网络错误!',
                  type: 'error'
                });
              })
        }).catch(() => {
          this.$message({
            message: '网络错误!',
            type: 'error'
          });
        });
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        //弹出界面
        this.saveFormVisible = true;
        this.saveForm = row;
        console.log("回显信息")
        console.log(this.saveForm)
        this.getPreferentialMethodIds();
			},

			//显示新增界面
			handleAdd: function () {
        //弹出界面
        this.saveFormVisible = true;
        //初始化
        this.saveForm = {
          id:null,
          name:'',
          type: null,
          preferentialMethodId:null,
          dictionaryitem:null,
          startDate:'',
          endDate:'',
          description:'',

          createTime:'',
          createBy:'',
          editTime:'',
          editBy:'',
        }
        this.getPreferentialMethodIds();
      },
			//保存
      saveSubmit: function () {
        this.$refs.saveForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.saveLoading = true;
              //NProgress.start();
              let para = Object.assign({}, this.saveForm);

              if(para.typeId){
                para.typeId = para.typeId[para.typeId.length-1];
              }

              /*console.log("保存的数据")
              console.log(para)
              return ;*/
              this.$http.put("/activity",para)
                  .then(result => {
                    result = result.data;
                    if(result.success){
                      this.advancedQuery();

                      this.saveFormVisible = false;
                      this.$message({ message: '保存成功',type: 'success'});
                    }else{
                      this.$message({ message: result.message,type: 'error'});
                    }
                    this.saveLoading = false;

                  })
            });
          }
        });
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
      batchDelete: function () {
        //从sels中获取所有被选中的id
        var ids = this.sels.map(item => item.id);
        this.$confirm('数据将无法恢复!', '警告', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //除get请求其他请求传参应该加上requestBody否侧无数据
          this.$http.patch("/activity",ids)
              .then(result => {
                //无论成功与否都要关闭加载窗口
                this.listLoading = false;
                //后端返回的数据会被包在data中
                result = result.data;
                if(result.success){
                  this.$message({
                    message: '删除成功!',
                    type: 'success'
                  });
                  //刷新页面
                  this.advancedQuery();
                }else{
                  this.$message({
                    message: '删除失败!',
                    type: 'error'
                  });
                }
              })
              .catch(result => {
                this.$message({
                  message: '网络错误!',
                  type: 'error'
                });
              })
        }).catch(() => {
          this.$message({
            message: '网络错误!',
            type: 'error'
          });
        });
      },
		},
		mounted() {
			this.getActivitys();
		}
	}

</script>

<style scoped>

</style>